package com.sls.game.parser.ebaimi.zhifubao;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.ebaimi.ZhifubaoReceiveMsgInfo;

/**
 * 
 * @Description：易百米支付宝交易返回消息解析
 * @author Wangxuegang
 * @Date：2013-5-9
 * 
 */
public class EbaimiTradeParse {

	private static Logger log = Logger.getLogger(EbaimiTradeParse.class);

	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static ZhifubaoReceiveMsgInfo parseTrade(String xmlStr) {
		ZhifubaoReceiveMsgInfo info = new ZhifubaoReceiveMsgInfo();
		InputSource source = null;
		StringReader read = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);
			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("oid_goodsorder".equals(node.getNodeName())) { // 第二层节点
					String oid_goodsorder = node.getTextContent();
					info.setOid_goodsorder(oid_goodsorder);
				} else if ("sta_goodsbill".equals(node.getNodeName())) {
					String sta_goodsbill = node.getTextContent();
					info.setSta_goodsbill(sta_goodsbill);
				} else if ("oid_reguser".equals(node.getNodeName())) {
					String oid_reguser = node.getTextContent();
					info.setOid_reguser(oid_reguser);
				} else if ("jno_cli".equals(node.getNodeName())) {
					String jno_cli = node.getTextContent();
					info.setJno_cli(jno_cli);
				} else if ("name_goods".equals(node.getNodeName())) {
					String name_goods = node.getTextContent();
					info.setName_goods(name_goods);
				} else if ("dt_create".equals(node.getNodeName())) {
					String dt_create = node.getTextContent();
					info.setDt_create(dt_create);
				} else if ("jno_svr".equals(node.getNodeName())) {
					String jno_svr = node.getTextContent();
					info.setJno_svr(jno_svr);
				} else if ("retcode".equals(node.getNodeName())) {
					String retcode = node.getTextContent();
					info.setRetcode(retcode);
				} else if ("retmsg".equals(node.getNodeName())) {
					String retmsg = node.getTextContent();
					info.setRetmsg(retmsg);
				}
			}

			return info;
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

}
